package haoyou.csnf.manager.module.pm.controller.admin.professionaltitle;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import haoyou.csnf.manager.framework.common.pojo.PageParam;
import haoyou.csnf.manager.framework.common.pojo.PageResult;
import haoyou.csnf.manager.framework.common.pojo.CommonResult;
import haoyou.csnf.manager.framework.common.util.object.BeanUtils;
import static haoyou.csnf.manager.framework.common.pojo.CommonResult.success;

import haoyou.csnf.manager.framework.excel.core.util.ExcelUtils;

import haoyou.csnf.manager.framework.apilog.core.annotation.ApiAccessLog;
import static haoyou.csnf.manager.framework.apilog.core.enums.OperateTypeEnum.*;

import haoyou.csnf.manager.module.pm.controller.admin.professionaltitle.vo.*;
import haoyou.csnf.manager.module.pm.dal.dataobject.professionaltitle.ProfessionalTitleDO;
import haoyou.csnf.manager.module.pm.service.professionaltitle.ProfessionalTitleService;

@Tag(name = "管理后台 - 系统职称")
@RestController
@RequestMapping("/pm/professional-title")
@Validated
public class ProfessionalTitleController {

    @Resource
    private ProfessionalTitleService professionalTitleService;

    @PostMapping("/create")
    @Operation(summary = "创建系统职称")
    @PreAuthorize("@ss.hasPermission('pm:professional-title:create')")
    public CommonResult<Long> createProfessionalTitle(@Valid @RequestBody ProfessionalTitleSaveReqVO createReqVO) {
        return success(professionalTitleService.createProfessionalTitle(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新系统职称")
    @PreAuthorize("@ss.hasPermission('pm:professional-title:update')")
    public CommonResult<Boolean> updateProfessionalTitle(@Valid @RequestBody ProfessionalTitleSaveReqVO updateReqVO) {
        professionalTitleService.updateProfessionalTitle(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除系统职称")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('pm:professional-title:delete')")
    public CommonResult<Boolean> deleteProfessionalTitle(@RequestParam("id") Long id) {
        professionalTitleService.deleteProfessionalTitle(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除系统职称")
                @PreAuthorize("@ss.hasPermission('pm:professional-title:delete')")
    public CommonResult<Boolean> deleteProfessionalTitleList(@RequestParam("ids") List<Long> ids) {
        professionalTitleService.deleteProfessionalTitleListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得系统职称")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('pm:professional-title:query')")
    public CommonResult<ProfessionalTitleRespVO> getProfessionalTitle(@RequestParam("id") Long id) {
        ProfessionalTitleDO professionalTitle = professionalTitleService.getProfessionalTitle(id);
        return success(BeanUtils.toBean(professionalTitle, ProfessionalTitleRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得系统职称分页")
    @PreAuthorize("@ss.hasPermission('pm:professional-title:query')")
    public CommonResult<PageResult<ProfessionalTitleRespVO>> getProfessionalTitlePage(@Valid ProfessionalTitlePageReqVO pageReqVO) {
        PageResult<ProfessionalTitleDO> pageResult = professionalTitleService.getProfessionalTitlePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ProfessionalTitleRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出系统职称 Excel")
    @PreAuthorize("@ss.hasPermission('pm:professional-title:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportProfessionalTitleExcel(@Valid ProfessionalTitlePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ProfessionalTitleDO> list = professionalTitleService.getProfessionalTitlePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "系统职称.xls", "数据", ProfessionalTitleRespVO.class,
                        BeanUtils.toBean(list, ProfessionalTitleRespVO.class));
    }

}